package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IRqqyjyxkPzService;
import com.augurit.gzcgwsp.rqqyjyxk.service.IYwRqQyjyxkService;
import com.augurit.gzcgwsp.rqqyjyxk.web.form.YwRqQyjyxkForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 解析燃气经营许可--瓶装xml数据
 * 
 * @author Administrator
 * 
 */

// 1.map里面的 会办部门 字段 还没有处理
// 2.yslNo 还没有加上去
@Service
@Transactional
public class RqqyjyxkPzServiceImpl implements IRqqyjyxkPzService {
	Logger logger = (Logger) LoggerFactory.getLogger(RqqyjyxkPzServiceImpl.class);

	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwRqQyjyxkService ywRqQyjyxkService;
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String jyxkpzfilepath = localResource.getString("jyxkpzfilepath");

	// 附件存放路径
	private final String jyxkpzfujianpath = localResource.getString("jyxkpzfujianpath");

	// 解析成功反馈xml存放路径
	private final String jyxkpzfeedbackPath = localResource.getString("jyxkpzfeedbackPath");

	// 解析异常xml存放路径
	private final String jyxkpzexceptionPath = localResource.getString("jyxkpzexceptionPath");

	// 解析后，xml备份
	private final String jyxkpzbackPath = localResource.getString("jyxkpzbackPath");

	private Map<String, String> jyxkpzKeyMap;// xml内容的key

	private Map<String, String> jyxkpzKeyMapHead;// xml头部的key

	private Map<String, Long> jyxkpzfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initjyxkpzMap() {
		// 初始化数据部分对应的map
		jyxkpzKeyMap = new HashMap<String, String>();
		jyxkpzKeyMap.put("YSL_NO", "YSL_NO");
		jyxkpzKeyMap.put("approvalNo", "approvalNo");
		jyxkpzKeyMap.put("businessCode", "businessCode");
		jyxkpzKeyMap.put("sendTime", "sendTime");
		
		jyxkpzKeyMap.put("SQDW", "SQDW");
		jyxkpzKeyMap.put("ADDRESS", "ADDRESS");
		jyxkpzKeyMap.put("YZBM", "YZBM");
		jyxkpzKeyMap.put("CLSJ", "CLSJ");
		jyxkpzKeyMap.put("YYZZZCH", "YYZZZCH");
		jyxkpzKeyMap.put("LXR", "LXR");
		jyxkpzKeyMap.put("LXR_DH", "LXR_DH");
		jyxkpzKeyMap.put("CZ", "CZ");
		jyxkpzKeyMap.put("QYWZ", "QYWZ");
		jyxkpzKeyMap.put("DZYX", "DZYX");
		jyxkpzKeyMap.put("FDDBR", "FDDBR");
		jyxkpzKeyMap.put("FDDBR_ZW", "FDDBR_ZW");
		jyxkpzKeyMap.put("FDDBR_ZC", "FDDBR_ZC");
		jyxkpzKeyMap.put("ZYFZR", "ZYFZR");
		jyxkpzKeyMap.put("ZYFZR_ZW", "ZYFZR_ZW");
		jyxkpzKeyMap.put("ZYFZR_ZC", "ZYFZR_ZC");
		jyxkpzKeyMap.put("AQFZR", "AQFZR");
		jyxkpzKeyMap.put("AQFZR_ZW", "AQFZR_ZW");
		jyxkpzKeyMap.put("AQFZR_ZC", "AQFZR_ZC");
		jyxkpzKeyMap.put("JSFZR", "JSFZR");
		jyxkpzKeyMap.put("JSFZR_ZW", "JSFZR_ZW");
		jyxkpzKeyMap.put("JSFZR_ZC", "JSFZR_ZC");
		jyxkpzKeyMap.put("QYZCZB", "QYZCZB");
		jyxkpzKeyMap.put("QYLX", "QYLX");
		jyxkpzKeyMap.put("JYFW", "JYFW");
		jyxkpzKeyMap.put("GQQY", "GQQY");
		jyxkpzKeyMap.put("FWDH", "FWDH");
		jyxkpzKeyMap.put("QXDH", "QXDH");
		jyxkpzKeyMap.put("QYJJ", "QYJJ");

		

		// 初始化头部对应的map
		jyxkpzKeyMapHead = new HashMap<String, String>();
		jyxkpzKeyMapHead.put("sn", "sn");
		jyxkpzKeyMapHead.put("sender", "sender");
		jyxkpzKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		jyxkpzfujianMap = new HashMap<String, Long>();
		jyxkpzfujianMap.put("FJ_01", 97l);
		jyxkpzfujianMap.put("FJ_02", 74l);
		jyxkpzfujianMap.put("FJ_03", 75l);
		jyxkpzfujianMap.put("FJ_04", 76l);
		jyxkpzfujianMap.put("FJ_05", 77l);
		jyxkpzfujianMap.put("FJ_06", 78l);
		jyxkpzfujianMap.put("FJ_07", 79l);
		jyxkpzfujianMap.put("FJ_08", 80l);
		jyxkpzfujianMap.put("FJ_09", 81l);
		jyxkpzfujianMap.put("FJ_10", 82l);
		jyxkpzfujianMap.put("FJ_11", 83l);
		jyxkpzfujianMap.put("FJ_12", 84l);
		jyxkpzfujianMap.put("FJ_13", 85l);
		jyxkpzfujianMap.put("FJ_14", 86l);
		jyxkpzfujianMap.put("FJ_15", 87l);
		jyxkpzfujianMap.put("FJ_16", 88l);
		jyxkpzfujianMap.put("FJ_17", 89l);
		jyxkpzfujianMap.put("FJ_18", 90l);
		jyxkpzfujianMap.put("FJ_19", 98l);
	}

	public void readXml(String projectPath) {
		if (jyxkpzKeyMap == null)
			this.initjyxkpzMap();
		File file = new File(jyxkpzfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(jyxkpzKeyMap, filePath,jyxkpzfeedbackPath, jyxkpzexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(jyxkpzKeyMapHead,files[i], jyxkpzfeedbackPath, jyxkpzexceptionPath);

						// 把数据部分保存到数据库
						YwRqQyjyxkForm form = new YwRqQyjyxkForm();
						for (String mapKey : jyxkpzKeyMap.keySet()) {							
							form.setYslNo(parseXMLService.getMapValue(jyxkpzKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(jyxkpzKeyMap.get("SQDW")));							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(jyxkpzKeyMap.get("SQDW"),"rqjyxkz_pzqy");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(jyxkpzKeyMap.get("ADDRESS")));
							form.setPost(parseXMLService.getMapValue(jyxkpzKeyMap.get("YZBM")));
							String clsjTemp = parseXMLService.getMapValue(jyxkpzKeyMap.get("CLSJ"));
							Date clsj = null;
							if (org.apache.commons.lang.StringUtils.isNotEmpty(clsjTemp)) {
								clsj = DateUtils.stringToDate(clsjTemp,DateUtils.YYYY_MM_DD);
								form.setClsj(clsj);
							}
							
							form.setGszcdjh(parseXMLService.getMapValue(jyxkpzKeyMap.get("YYZZZCH")));
							form.setYwlxr(parseXMLService.getMapValue(jyxkpzKeyMap.get("LXR")));
							form.setYwlxrtel(parseXMLService.getMapValue(jyxkpzKeyMap.get("LXR_DH")));
							form.setCz(parseXMLService.getMapValue(jyxkpzKeyMap.get("CZ")));
							form.setQywz(parseXMLService.getMapValue(jyxkpzKeyMap.get("QYWZ")));
							form.setEmail(parseXMLService.getMapValue(jyxkpzKeyMap.get("DZYX")));
							form.setFddbr(parseXMLService.getMapValue(jyxkpzKeyMap.get("FDDBR")));
							form.setFddbrZw(parseXMLService.getMapValue(jyxkpzKeyMap.get("FDDBR_ZW")));
							form.setFddbrZc(parseXMLService.getMapValue(jyxkpzKeyMap.get("FDDBR_ZC")));
							form.setZyfzr(parseXMLService.getMapValue(jyxkpzKeyMap.get("ZYFZR")));
							form.setZyfzrZw(parseXMLService.getMapValue(jyxkpzKeyMap.get("ZYFZR_ZW")));
							form.setZyfzrZc(parseXMLService.getMapValue(jyxkpzKeyMap.get("ZYFZR_ZC")));
							form.setAqfzr(parseXMLService.getMapValue(jyxkpzKeyMap.get("AQFZR")));
							form.setAqfzrZw(parseXMLService.getMapValue(jyxkpzKeyMap.get("AQFZR_ZW")));
							form.setAqfzrZc(parseXMLService.getMapValue(jyxkpzKeyMap.get("AQFZR_ZC")));
							form.setJsfzr(parseXMLService.getMapValue(jyxkpzKeyMap.get("JSFZR")));
							form.setJsfzrZw(parseXMLService.getMapValue(jyxkpzKeyMap.get("JSFZR_ZW")));
							form.setJsfzrZc(parseXMLService.getMapValue(jyxkpzKeyMap.get("JSFZR_ZC")));
							form.setZczj(parseXMLService.getMapValue(jyxkpzKeyMap.get("QYZCZB")));
							form.setSqrq(new Date());
							
							String qylxTemp = parseXMLService.getMapValue(jyxkpzKeyMap.get("QYLX"));
							Long qylx = null;
							if (StringUtils.isNotEmpty(qylxTemp)) {
								qylx = Long.parseLong(qylxTemp);
								form.setQylx(qylx);
							}
							form.setSarq(new Date());
							
							form.setJyfw(parseXMLService.getMapValue(jyxkpzKeyMap.get("JYFW")));
							form.setGqqy(parseXMLService.getMapValue(jyxkpzKeyMap.get("GQQY")));
							form.setFwdh(parseXMLService.getMapValue(jyxkpzKeyMap.get("FWDH")));
							form.setQxdh(parseXMLService.getMapValue(jyxkpzKeyMap.get("QXDH")));
							form.setQyjj(parseXMLService.getMapValue(jyxkpzKeyMap.get("QYJJ")));
							
							
							form.setBranch("rqjyxkz_pzqy");
							form.setApprovalNo(parseXMLService.getMapValue(jyxkpzKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(jyxkpzKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(jyxkpzKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywRqQyjyxkService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									jyxkpzKeyMapHead, jyxkpzfeedbackPath, jyxkpzexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, jyxkpzfujianpath, jyxkpzfeedbackPath, jyxkpzexceptionPath, jyxkpzKeyMapHead,
								files, jyxkpzfujianMap, i, form.getQyid(), form.getId(), "rqjyxkz_pzqy");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								jyxkpzKeyMapHead, jyxkpzfeedbackPath, jyxkpzexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, jyxkpzbackPath + fileName,fileName);
				}
			}
		}
	}

}
